package com.lh.api.controller;

import com.lh.api.service.AppVersionService;
import com.lh.api.vo.VersionVo;
import com.lh.common.entity.admin.AppVersion;
import com.lh.common.enums.AppTypeEnum;
import com.lh.common.enums.IF;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api(value= "AppAction",tags={"APP相关的接口"})
public class AppAction extends AbstractController{

	@Autowired
	private AppVersionService appVersionService;

	@ApiOperation(value = "APP版本接口")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query", name = "appType", value = "客户端类型IOS或者ANDROID", required = true,dataTypeClass= AppTypeEnum.class),
			@ApiImplicitParam(paramType="query", name = "versionCode", value = "APP当前版本，形如v1.0.0则传100", required = true,dataTypeClass= Integer.class)
	})
	@GetMapping("/version")
	@ResponseBody
	public Result<VersionVo> version(AppTypeEnum appType, Integer versionCode) {

		AssertUtil.assertNotNull(appType, Constant.RESULT.FI1000, "appType is required");
		AssertUtil.assertNotNull(versionCode, Constant.RESULT.FI1000, "versionCode is required");

		VersionVo version = new VersionVo();
		AppVersion appVersion = appVersionService.getNewAppVersion(appType.getValue(),versionCode);

		if(appVersion == null) return Result.ok(version);
		version.setNewVersion(appVersion.getVersion());
		version.setNewVersionCode(appVersion.getVersionCode());
		version.setUpdateDescription(appVersion.getUpdateInfo());
		version.setIsUpdate(appVersion.getForceUpdate());
		version.setApkUrl(appVersion.getApkUrl());
		return Result.ok(version);
	}
}
